package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DAreaMappedService;
import cn.huiyunche.base.service.mappers.DAreaMappedMapper;
import cn.huiyunche.base.service.model.DAreaMapped;
import cn.huiyunche.base.service.model.DAreaMappedExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.tms.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/19 下午1:40
 */
@Service
public class DAreaMappedServiceImpl implements DAreaMappedService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DAreaMappedServiceImpl.class);

    @Autowired
    private DAreaMappedMapper dAreaMappedMapper;

    @Override
    public DAreaMapped getByAreaCode(String areaCode) {
        LOGGER.info("getByAreaCode param : {}", areaCode);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("getByAreaCode param areaCode must not null");
            throw new IllegalArgumentException("区域编码不能为空");
        }

        DAreaMappedExample example = new DAreaMappedExample();
        example.createCriteria().andAreaCodeEqualTo(areaCode);

        List<DAreaMapped> list = dAreaMappedMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public DAreaMapped getByAreaName(String areaName) {
        LOGGER.info("getByAreaName param : {}", areaName);
        if (StringUtils.isBlank(areaName)) {
            LOGGER.error("getByAreaCode param areaName must not null");
            throw new IllegalArgumentException("区域名称不能为空");
        }

        DAreaMappedExample example = new DAreaMappedExample();
        example.createCriteria().andAreaNameEqualTo(areaName);

        List<DAreaMapped> list = dAreaMappedMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }
}
